package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.fragment.InterviewFragment;
import com.example.myapplication.fragment.MineFragment;
import com.example.myapplication.fragment.ProjectFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

public class BottomNavgationActivity extends AppCompatActivity {
    private List<Fragment> list;
    private BottomNavigationView bottomnavgation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bottom_navgation);
        View fragment = findViewById(R.id.fragment_container);
        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new ProjectFragment());
        list.add(new InterviewFragment());
        list.add(new MineFragment());
        showFragment(0);
        bottomnavgation = findViewById(R.id.bottom_navigation);
        bottomnavgation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                Log.d("BottomNavgationActivity", "onNavigationItemSelected: "+menuItem.getItemId());
                int id =menuItem.getItemId();
                if (id == R.id.menu_home){
                    showFragment(0);
                }else if (id == R.id.menu_project){
                    showFragment(1);
                }else if (id == R.id.menu_interview){
                    showFragment(2);
                }else if (id == R.id.menu_mine){
                    showFragment(3);
                }
                return true;
            }
        });
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
    private void showFragment(int position){
        Log.d("BottomNavgationActivity", "showFragment: "+position);
        //获取框架管理者，然后开启事务，替换容器，提交事务
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container,list.get(position))
                .commit();
    }
}